﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using ZX.AspNetComet.Core;

namespace Server.Channels
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ChatService
    {
       
        /// <summary>
        /// 发送用户消息
        /// </summary>
        /// <param name="senderID">发送者ID</param>
        /// <param name="receiverID">接收者ID</param>
        /// <param name="title">标题</param>
        /// <param name="message">消息</param>
        /// <param name="extensionProperties">扩展属性(例如:跟该消息相关的功能url)</param>
        [OperationContract]
        public void SendMessage(
            string senderID, 
            string receiverID,
            string title,
            string message,
            string extensionProperties)
        {
            ChatMessage chatMessage = new ChatMessage();
            CometClient cometClient = DefaultChannelHandler.StateManager.GetCometClient(senderID);
            chatMessage.From = cometClient.UserName;
            chatMessage.Message = message;

            DefaultChannelHandler.StateManager.SendMessage(receiverID,title, chatMessage, extensionProperties);
        }

        /// <summary>
        /// 发送公告消息
        /// </summary>
        /// <param name="senderID">发送者ID</param>
        /// <param name="title">标题</param>
        /// <param name="message">消息</param>
        /// <param name="extensionProperties">扩展属性(例如:跟该消息相关的功能url)</param>
        [OperationContract]
        public void SendSystemMessage(
            string senderID, 
            string title,
            string message,
            string extensionProperties)
        {
            CometClient cometClient = DefaultChannelHandler.StateManager.GetCometClient(senderID);
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.From = cometClient.UserName;
            chatMessage.Message = message;
            DefaultChannelHandler.StateManager.SendMessage(title, chatMessage, extensionProperties);
        }


        /// <summary>
        /// 群发消息
        /// </summary>
        /// <param name="senderID">发送者ID</param>
        /// <param name="receiverIDs">接收者ID列表</param>
        /// <param name="title">标题</param>
        /// <param name="message">消息</param>
        /// <param name="extensionProperties">扩展属性(例如:跟该消息相关的功能url)</param>
        [OperationContract]
        public void SendGroupMessage(
            string senderID,
            string[] receiverIDs,
            string title,
            string message,
            string extensionProperties)
        {
            CometClient cometClient = DefaultChannelHandler.StateManager.GetCometClient(senderID);
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.From = cometClient.UserName;
            chatMessage.Message = message;
            DefaultChannelHandler.StateManager.SendMessage(receiverIDs,title, chatMessage, extensionProperties);
        }
    }
}
